package org.aileen.springbook.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Slf4j
@Aspect
public class TimeRecordAspect {
@Around("execution(* org.aileen.springbook.controller.*.*(..))")
    public Object timeRecord(ProceedingJoinPoint joinPoint) throws Throwable {
    //1.记录开始时间
    //2.执行目标方法
    //3.记录结束时间
    //4.记录耗时
        long start = System.currentTimeMillis();
        log.info("do Around 前");
        Object result = joinPoint.proceed(); // 执行目标方法
        long end = System.currentTimeMillis();
        log.info("do Around 后");
        log.info("方法 {} 执行时间: {} ms", joinPoint.getSignature(), (end - start));
        return result;
    }

    @Before("execution(* org.aileen.springbook.controller.*.*(..))")
    public void doBefore(JoinPoint joinPoint){
        log.info(joinPoint.getSignature().toShortString());
        log.info("执行before方法...");
    }

    @After("execution(* org.aileen.springbook.controller.*.*(..))")
    public void doAfter(){
        log.info("执行after方法...");
    }

    @AfterThrowing("execution(* org.aileen.springbook.controller.*.*(..))")
    public void doAfterThrowing(){
        log.info("执行doAfterThrowing方法...");
    }

    @AfterReturning("execution(* org.aileen.springbook.controller.*.*(..))")
    public void doAfterReturing(){
        log.info("执行doAfterReturing方法...");
    }

}
